<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>注册页面</title>
    <link rel="stylesheet" href="styles.css">
</head>
<body>
    <div class="container">
        <h2>注册</h2>
        <form id="registerForm">
            <div class="form-group">
                <label for="username">用户名:</label>
                <input type="text" id="username" name="username" required>
            </div>
            <div class="form-group">
                <label for="password">密码:</label>
                <input type="password" id="password" name="password" required>
            </div>
            <button type="submit">注册</button>
        </form>
    </div>

    <script>
        document.getElementById('registerForm').addEventListener('submit', function(event) {
            event.preventDefault(); // 防止默认提交

            // 获取表单数据
            const username = document.getElementById('username').value;
            const password = document.getElementById('password').value;

            // 创建POST请求
            const xhr = new XMLHttpRequest();
            xhr.open('POST', 'register', true);
            xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

            // 发送请求
            xhr.send(`username=${encodeURIComponent(username)}&password=${encodeURIComponent(password)}`);

            // 处理响应
            xhr.onload = function() {
                if (xhr.status === 200) {
                    alert('注册成功！');
                } else {
                    alert('注册失败，请重试。');
                }
            };
        });
    </script>
</body>
</html>
